package org.si.icon;

import java.awt.*;
import org.si.graphics.BackgroundPaintProvider;
import org.si.graphics.FixedPaintProvider;
import org.si.graphics.PaintProvider;

/**
 * Defines a simple icon that uses a Paint instance to fill the icon area.
 *
 * @author Matt Nathan
 */
public class MatteIcon extends AbstractScalableIcon {

    private final int width;
    private final int height;
    private final PaintProvider provider;





    /**
     * Create a new MatteIcon that paints the icon area with the given components background colour.
     *
     * @param width  The default width of this icon.
     * @param height The default height of this icon.
     */
    public MatteIcon(int width, int height) {
        this(width, height, new BackgroundPaintProvider());
    }





    /**
     * Create a new MatteIcon that will use the given Paint to fill it's area.
     *
     * @param width  The default width for this icon.
     * @param height The default height for this icon.
     * @param paint  The Paint to fill the target area with.
     */
    public MatteIcon(int width, int height, Paint paint) {
        this(width, height, new FixedPaintProvider(paint));
    }





    /**
     * Create a new MatteIcon that will use the Paint provided by the given PaintProvider to fill the target area.
     *
     * @param width    The default width for this icon.
     * @param height   The default height for this icon.
     * @param provider The provider of Paint instances to fill the target paint area.
     */
    public MatteIcon(int width, int height, PaintProvider provider) {
        this.width = width;
        this.height = height;
        this.provider = provider;
    }





    /**
     * This icon always fills the entire target area.
     *
     * @param c      The component that this will be painted to.
     * @param width  The target width for the icon paint area.
     * @param height The target height for the icon paint area.
     * @param result The place to put the size the icon is hapy painting in.
     */
    @Override
    public void fitInto(Component c, int width, int height, Dimension result) {
        result.setSize(width, height);
    }





    /**
     * Fill the target area with a paint that was provided in the constructor.
     *
     * @param c      The component which caused the painting.
     * @param g      The graphics device to paint to.
     * @param x      The x coordinate for the icon to paint at.
     * @param y      The y coordinate for the icon to paint at.
     * @param width  The target width for the icon.
     * @param height The target height for the icon.
     */
    @Override
    public void paintIcon(Component c, Graphics g, int x, int y, int width, int height) {
        Paint paint = provider.get(c, x, y, width, height);
        if (paint != null) {
            Graphics2D g2 = (Graphics2D) g.create();
            g2.setPaint(paint);
            g2.fillRect(x, y, width, height);
            g2.dispose();
        }
    }





    /** {@inheritDoc} */
    @Override
    public int getIconWidth() {
        return width;
    }





    /** {@inheritDoc} */
    @Override
    public int getIconHeight() {
        return height;
    }

}
